1. /* scommult.cpp by K.Tsuru */
  2. // function ID = 903
  3. /********************************************
  4. SComplex class
  5. multiplication (*this)*z = (a + bi)*(c + di)
  6. ********************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. SComplex& SComplex::operator*=(const SComplex& z) {
  11. if ( IsZero(903) ) return *this; // a = b= 0
  12. if ( z.IsZero(903) ) { // c = d = 0
  13. SetZero(); return *this;
  14. }
  15. // d = 0, (a+bi)*c
  16. if (z.im.Sign(903) == SNumber::ZERO) return (*this) *= z.re; // d=0 [operator*=(const SDouble& x) is used]
  17. // c = 0, (a+bi)*di = -b*d +(a*d)i
  18. if (z.re.Sign(903) == SNumber::ZERO) { // c=0, (a+bi)*di = -bd+i ad = -im*z.im + i re*z.im
  19. const SDouble r = -im * z.im;
  20. im = re * z.im;
  21. re = r;
  22. return *this;
  23. }
  24. // if(&x == &y) { // z * z can be speed-up by z = a + ib, z^2 = a^2-b^2 + 2iab
  25. if(*this == z) { // Overhead is very small comparing above.
  26. SDouble temp(0.0); // (a+ai)^2 = 2a^2i
  27. if(re != im) temp = re * re - im * im;
  28. im = re * im; im = DsMult(im, 2);
  29. re = temp;
  30. return *this;
  31. } else {
  32. /*******************************
  33. (a + bi)*(c + di) = a*c - b*d +(a*d + b*c)i
  34. = a*(c -d) + d*(a -b)+{b*(c+d) + d*(a -b)}i
  35. = (re + i*im)*(z.re + i*z.im)
  36. Let re = a, im = b, z.re = c, z.im = d, temp = d*(a -b).
  37. ***********************************/
  38. const SDouble temp = z.im * (re - im); // three times *operator()
  39. re = re * (z.re - z.im) + temp; // a*(c-d)+temp
  40. im = im * (z.re + z.im) + temp; // b*(c+d)+temp
  41. return *this;
  42. }
  43. }

scommult.cpp : last modifiled at 2016/08/07 15:29:02(1,539 bytes)
created at 2017/10/06 15:21:28
The creation time of this html file is 2017/10/06 15:27:09 (Fri Oct 06 15:27:09 2017).